package com.yanqu.road.dao.impl.activity.suitang.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangAutoSettingData;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUserData;
import com.yanqu.road.entity.activity.suitang.data.SuiTangUserData;
import com.yanqu.road.entity.activity.suitang.entity.barracks.CrossSuiTangBarracks;
import com.yanqu.road.entity.activity.suitang.entity.team.CrossSuiTangTeam;
import com.yanqu.road.entity.activity.suitang.entity.user.CrossSuiTangUserBaseInfo;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSuiTangUserDataDaoImpl extends TempDataObjectDao<CrossSuiTangUserData> {
    public static String getCrossSuiTangUserDataSql = "select * from `t_c_suitang_user_data` where `activityId`=? and `groupId`=?";
    public Map<Long, CrossSuiTangUserData> getCrossSuiTangUserData(int activityId,int groupId) {
        Map<Long, CrossSuiTangUserData> userDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossSuiTangUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSuiTangUserData userData = parse(rs);
                userDataMap.put(userData.getUserId(), userData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return userDataMap;
    }

    public static String updateCrossSuiTangUserDataSql = "update `t_c_suitang_user_data` set unionUid=?,patronsTotalAbility=?,patronsNSum=?,patronsTotalNum=?,soldierAddition=?,team=?,barracks=?,userBaseInfo=?,userRoundMap=?,positionId=?,zhanGong=?,zhanGongTime=?,receiveJunZi=?,curJunZi=?,redDotMap=?,horseId=?,autoSettingData=?,waitReward=? where activityId=? and groupId=? and userId=?";
    @Override
    protected boolean updateData(CrossSuiTangUserData crossSuiTangUserData) {
        crossSuiTangUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangUserData.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getPatronsTotalAbility()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getPatronsNSum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getPatronsTotalNum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getSoldierAddition()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getTeam())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getBarracks())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getUserRoundMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getPositionId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getZhanGong()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getZhanGongTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getReceiveJunZi()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getCurJunZi()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getRedDotMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getHorseId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getAutoSettingData())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(crossSuiTangUserData.getWaitReward())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossSuiTangUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSuiTangUserDataSql = "insert into `t_c_suitang_user_data`(activityId,groupId,userId,unionUid,patronsTotalAbility,patronsNSum,patronsTotalNum,soldierAddition,team,barracks,userBaseInfo,userRoundMap,positionId,zhanGong,zhanGongTime,receiveJunZi,curJunZi,redDotMap,horseId,autoSettingData,waitReward) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossSuiTangUserData crossSuiTangUserData) {
        crossSuiTangUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangUserData.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getPatronsTotalAbility()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getPatronsNSum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getPatronsTotalNum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getSoldierAddition()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getTeam())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getBarracks())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getUserRoundMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getPositionId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getZhanGong()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUserData.getZhanGongTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getReceiveJunZi()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getCurJunZi()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getRedDotMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUserData.getHorseId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUserData.getAutoSettingData())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(crossSuiTangUserData.getWaitReward())));
        if (getDb().executeNoneQuery(insertCrossSuiTangUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSuiTangUserData parseData(ResultSet rs) throws SQLException {
        CrossSuiTangUserData crossSuiTangUserData = new CrossSuiTangUserData();
        crossSuiTangUserData.setActivityId(rs.getInt("activityId"));
        crossSuiTangUserData.setGroupId(rs.getInt("groupId"));
        crossSuiTangUserData.setUserId(rs.getLong("userId"));
        crossSuiTangUserData.setUnionUid(rs.getString("unionUid"));
        crossSuiTangUserData.setPatronsTotalAbility(rs.getLong("patronsTotalAbility"));
        crossSuiTangUserData.setPatronsNSum(rs.getLong("patronsNSum"));
        crossSuiTangUserData.setPatronsTotalNum(rs.getLong("patronsTotalNum"));
        crossSuiTangUserData.setSoldierAddition(rs.getLong("soldierAddition"));
        crossSuiTangUserData.setTeam(JSON.parseObject(rs.getString("team"), CrossSuiTangTeam.class));
        crossSuiTangUserData.setBarracks(JSON.parseObject(rs.getString("barracks"), CrossSuiTangBarracks.class));
        crossSuiTangUserData.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), CrossSuiTangUserBaseInfo.class));
        crossSuiTangUserData.setUserRoundMap(JSON.parseObject(rs.getString("userRoundMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.activity.suitang.entity.user.CrossSuiTangUserRound>>() {}));
        crossSuiTangUserData.setPositionId(rs.getInt("positionId"));
        crossSuiTangUserData.setZhanGong(rs.getLong("zhanGong"));
        crossSuiTangUserData.setZhanGongTime(rs.getLong("zhanGongTime"));
        crossSuiTangUserData.setReceiveJunZi(rs.getInt("receiveJunZi"));
        crossSuiTangUserData.setCurJunZi(rs.getInt("curJunZi"));
        crossSuiTangUserData.setRedDotMap(JSON.parseObject(rs.getString("redDotMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Long>>() {}));
        crossSuiTangUserData.setHorseId(rs.getInt("horseId"));

        // 加一个自动设置的
        String autoSettingData = rs.getString("autoSettingData");
        if (!StringUtils.isNullOrEmpty(autoSettingData)) {
            crossSuiTangUserData.setAutoSettingData(JSON.parseObject(autoSettingData, new TypeReference<CrossSuiTangAutoSettingData>() {

            }));
        }

        String waitReward = rs.getString("waitReward");
        if (!StringUtils.isNullOrEmpty(waitReward)) {
            crossSuiTangUserData.setWaitReward(PropertyHelper.parseStringToProperty(waitReward));
        }

        crossSuiTangUserData.setHorseId(rs.getInt("horseId"));

        return crossSuiTangUserData;
    }
}
